// JQuery Twitter Feed. Coded by www.webdevdoor.com (2012) and modified from https://twitter.com/javascripts/blogger.js
$(document).ready(function () {

    var displaylimit = 1;
    var twitterprofile = "";
    var screenname = "";
    var showdirecttweets = false;
    var showretweets = true;
    var showtweetlinks = true;
    var showprofilepic = true;

    var headerHTML = '';
    var loadingHTML = '';
    loadingHTML += '<div id="loading-container"><img src="img/loader_tweets.gif" style="float:right;" alt="Tweets loader" /></div>';

    $('#twitter-feed').html(headerHTML + loadingHTML);
    $('#twitter-feed-footer').html(headerHTML + loadingHTML);

    //Function modified from Stack Overflow
    function addlinks(data) {
        //Add link to all http:// links within tweets
        data = data.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function (url) {
            return '<a href="' + url + '" >' + url + '</a>';
        });

        //Add link to @usernames used within tweets
        data = data.replace(/\B@([_a-z0-9]+)/ig, function (reply) {
            return '<a href="http://twitter.com/' + reply.substring(1) + '" style="font-weight:lighter;" >' + reply.charAt(0) + reply.substring(1) + '</a>';
        });
        return data;
    }

    function relative_time(time_value) {
        var values = time_value.split(" ");
        time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
        var parsed_date = Date.parse(time_value);
        var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
        var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
        var shortdate = time_value.substr(4, 2) + " " + time_value.substr(0, 3);
        delta = delta + (relative_to.getTimezoneOffset() * 60);

        if (delta < 60) {
            return '1m';
        } else if (delta < 120) {
            return '1m';
        } else if (delta < (60 * 60)) {
            return (parseInt(delta / 60)).toString() + 'm';
        } else if (delta < (120 * 60)) {
            return '1h';
        } else if (delta < (24 * 60 * 60)) {
            return (parseInt(delta / 3600)).toString() + 'h';
        } else if (delta < (48 * 60 * 60)) {
            //return '1 day';
            return shortdate;
        } else {
            return shortdate;
        }
    }

});